﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using One.Framework.UI.Security;

namespace SH.LMS.Common.UI.Cookie
{
    public class CookieManager
    {
        public static string GetSubCookieValue(string cookieName, string key, CookieProtection Decode)
        {
            //string ret = "No Exist";
            HttpCookieCollection Cookies = HttpContext.Current.Request.Cookies;

            if (Cookies[cookieName] == null) return string.Empty;
            else
            {
                string ret = HttpUtility.UrlDecode(Cookies[cookieName][key]);

                if (Decode == CookieProtection.None)
                {
                    return ret;

                }
                else
                    return MachineKeyCryptography.Decode(ret, Decode);
            }
        }

        /// <summary>
        /// 쿠키 값을 지정된 쿠키보호 수준에 따라 디코딩해서 리턴한다.
        /// </summary>
        /// <param name="cookieName"></param>
        /// <param name="decode"></param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName, CookieProtection decode)
        {
            HttpCookieCollection Cookies = HttpContext.Current.Request.Cookies;
            string ret = HttpUtility.UrlDecode(Cookies[cookieName].Value);



            if (decode == CookieProtection.None)
            {
                return ret;

            }
            else
                return MachineKeyCryptography.Decode(ret, decode);

        }

        public static string GetCookieValue(Enum cookies)
        {
            string cookieName = Enum.GetName(cookies.GetType(), cookies);
            return GetCookieValue(cookieName);
        }


        /// <summary>
        /// 쿠키값을 그대로 리턴
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetCookieValue(string cookieName)
        {

            return GetCookieValue(cookieName, CookieProtection.None);
        }

        /// <summary>
        /// 암호화된 쿠키 값을 복호화 해서 리턴
        /// </summary>
        /// <param name="cookieName"></param>
        /// <returns></returns>
        public static string GetSecureCookieValue(string cookieName)
        {
            return GetCookieValue(cookieName, CookieProtection.All);
        }

        public static string GetSecureCookieValue(Enum cookies)
        {
            string cookieName = Enum.GetName(cookies.GetType(), cookies);
            return GetSecureCookieValue(cookieName);
        }


        public static void SetCookie(string cookieName, string value, string domain, CookieProtection encode)
        {
            if (encode != CookieProtection.None)
                value = MachineKeyCryptography.Encode(value, encode);

            HttpCookieCollection Cookies = HttpContext.Current.Response.Cookies;

            HttpCookie cookie = new HttpCookie(cookieName);
            cookie.Value = HttpUtility.UrlEncode(value);
            if (!string.IsNullOrEmpty(domain))
                cookie.Domain = domain;

            if (Cookies[cookieName] == null)
            {

                Cookies.Add(cookie);
            }
            else
            {
                Cookies.Set(cookie);
            }

        }

        public static void SetCookie(Enum cookies, string value, string domain, CookieProtection encode)
        {
            string cookieName = Enum.GetName(cookies.GetType(), cookies);
            SetCookie(cookieName, value, domain, encode);
        }

    }
}
